import base64
import json

import requests
from selenium import webdriver
import time


# 验证码识别
def base64_api(uname, pwd, img, typeid):
    with open(img, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        return result["message"]


driver = webdriver.Chrome()

# 打开古诗词网站
driver.get('https://so.gushiwen.cn')

driver.find_element_by_xpath('//div[@class="son1"]/a[6]').click()

# 账号
driver.find_element_by_xpath('//*[@id="email"]').send_keys('18504743924')

# 密码
driver.find_element_by_xpath('//*[@id="pwd"]').send_keys('123456')

# 定位图片位置
code = driver.find_element_by_xpath('//*[@id="imgCode"]')

# 截取验证码图片
code.screenshot('./img/img_path.png')

time.sleep(1)

# 识别验证码
result = base64_api(uname='18504743924', pwd='123456', img="./img/img_path.png", typeid=3)

# 等待识别完毕
time.sleep(1)

# 验证码
driver.find_element_by_xpath('//*[@id="code"]').send_keys(result)

# 登录
driver.find_element_by_xpath('//*[@id="denglu"]').click()
评论 ( 0 )